<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $this->pageTitle; ?></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="robots" content="noindex,nofollow" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/public/css/admin/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/extjs/resources/css/ext-all.css">
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/extjs/ext-all.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/adminCkeditor.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/adminExtJs.js"></script>

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/public/css/admin/grid.css" />
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/js/admin/gridCellActions.js"></script>

</head>
<body style="background: none;">
<div id="user-form"></div>
<div id="gridDiv"></div>
<script type="text/javascript">
var hotelId = <?php echo $hotelId ?>;
var store = null;
var grid  = null;

Ext.onReady(function() {

	var fm = Ext.form;

	//numeric
	Ext.form.VTypes['numericVal']  = /^[0-9]+$/i;
	Ext.form.VTypes['numeric'] = function(v) {
		return Ext.form.VTypes['numericVal'].test(v);
	};

	var action = new Ext.ux.grid.RowActions({
		header:'Действия',
		width: 70,
		autoWidth:false,
		hideMode:'display',
		keepSelection:true,
		actions:[{
			iconIndex: 'action',
			iconCls: 'icon-delete-item',
			callback: function(grid, record, action, groupId) {
				deleteRow(record.data.id);
			}
		}]
	});
	
	var cm = new Ext.grid.ColumnModel({
	    columns: [
	        {
				id: 'title',
	            header: 'Название',
	            dataIndex: 'title',
	            width: 220,
	            sortable: true,
	            menuDisabled: true,
	            editor: new fm.TextField({
					allowBlank: false
				})
	        },<?php $counter = 0;
	        		foreach ($columns as $item) {
	        			$hotelDates = $item->getRelated('hotelDates');
	        			
	        			if (count($hotelDates) > 0) {
	        				$counter++;
							echo '{';
							echo "id: 'dateField{$item->id}',";
							echo "width: 150,";
							echo "sortable: true,";
							echo "menuDisabled: true,";
							echo "header: '";
							foreach ($hotelDates as $subItem) {
								echo date('d.m.Y', strtotime($subItem->dateStart)) . ' - ' . date('d.m.Y', strtotime($subItem->dateEnd)) . '<br />'; 
							}
							echo "',";
							echo "dataIndex: 'dateField{$item->id}',";
							echo 'editor: new fm.NumberField({';
							echo 'allowNegative: false,';
	            			echo '})';
							echo '},';
						}
	        		}
			  ?> action
	    ]
	});

	store = new Ext.data.JsonStore({
	    root: 'data',
	    totalProperty: 'totalCount',
	    idProperty: 'id',
	    remoteSort: true,
	
	    fields: [
			{name: 'id'},
			{name: 'title', type: 'string'},
			<?php
			$counter = 0;
			foreach ($columns as $item) {
				$hotelDates = $item->getRelated('hotelDates');
				
				if (count($hotelDates) > 0) {
					$counter++;
					echo '{';
					echo "name: 'dateField{$item->id}',";
					echo "type: 'float'";
					echo '},';
				}
			}
	  		?>
			{name: 'action', type: 'string'}
	    ],

	    proxy: new Ext.data.HttpProxy({
	    	api: {
        		read : '/admin/hotelrooms/getdata/',
	        	update: '/admin/hotelrooms/updatedata/hotelId/' + hotelId
	    	}
	    }),

	    writer: new Ext.data.JsonWriter({
	        encode: true,
	        writeAllFields: false
	    })
	});
	store.setDefaultSort('title', 'asc');

	grid = new Ext.grid.EditorGridPanel({
	    store: store,
	    cm: cm,
	    width: '100%',
	    height: 500,
	    title: 'Комнаты и цена',
	    plugins: [action],
	    clicksToEdit: 1
	});

	var userForm = new Ext.form.FormPanel({
	    frame: true,
	    labelAlign: 'right',
	    title: 'Добавить строку',
	    frame: true,
	    width: '100%',
	    defaultType: 'textfield',

        items: [{
	            fieldLabel: 'Название',
	            name: 'title',
	            allowBlank:false,
	            width: 200
	        },<?php
			$counter = 0;
			foreach ($columns as $item) {
				$hotelDates = $item->getRelated('hotelDates');
				
				if (count($hotelDates) > 0) {
					$counter++;
					echo '{';
					echo "fieldLabel: 'Цена #{$counter}',";
					echo "name: 'dateField{$item->id}',";
					echo "vtype: 'numeric',";
					echo "width: 200";
					echo '},';
				}
			}
	  		?>{
	  			name: 'hotelId',
	  			value: hotelId,
	  			xtype: 'hidden'
	  		}
	    ],
	    buttonAlign: 'left',
    	buttons: [{
        	text: 'Сохранить',
        	handler: function(btn, ev) {
    			var fp = this.ownerCt.ownerCt, form = fp.getForm();
    			if (form.isValid()) {
					form.submit({
						url: '/admin/hotelrooms/add/',
						success: function(form, action){
							form.reset();
							var tmpObj = null;
							//eval('tmpObj = ' + action.response.responseText);

					        store.reload();
						}
					});
				}
    		}
    	},{
        	text: 'Очистить',
        	handler: function(btn, ev) {
    			var fp = this.ownerCt.ownerCt, form = fp.getForm();
				form.reset();
        	}
    	}]
    });

	userForm.render('user-form');
	
	store.load({params:{hotelId: hotelId}});
	grid.render('gridDiv');
});

function deleteRow(id)
{
	Ext.Ajax.request({
		url: '/admin/hotelrooms/delete/id/' + parseInt(id) + '/',
			success: function() {
				store.reload();
			},
		failure: function() {},
		disableCaching: false
	});
	
}
</script>
</body>
</html>